package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int insertRole(Role record) {
        roleMapper.insert(record);
        return 0;
    }

    @Override
    public int deleteRole(Long id) {
        roleMapper.deleteByPrimaryKey(id);
        roleMapper.deleteRolePerMiddleTable(id);
        return 0;
    }

    @Override
    public int updateRole(Role record) {
        return roleMapper.updateByPrimaryKey(record);
    }


    @Override
    public Role selectRole(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> selectRoleList() {
        return roleMapper.selectAll();
    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(roleMapper.queryList(qo));
    }

    @Override
    public void saveRoleAndPer(Role role, Long[] perIds) {
        roleMapper.insert(role);
        if (perIds != null && perIds.length != 0) {
            roleMapper.insertRolePerMiddleTable(role.getId(), perIds);
        }
    }

    @Override
    public void updateRoleAndPer(Role role, Long[] perIds) {
        roleMapper.updateByPrimaryKey(role);
        if (perIds != null && perIds.length != 0) {
            roleMapper.deleteRolePerMiddleTable(role.getId());
            roleMapper.insertRolePerMiddleTable(role.getId(), perIds);
        }
    }
}
